#include "channel.h"
#include "assert.h"

Channel::Channel(EventLoop* loop)
  : loop_(loop),
    fd_(-1),
    events_(0),
    revents_(0)
{}

Channel::Channel(EventLoop* loop, int fd__)
  : loop_(loop),
    fd_(fd__),
    events_(0),
    revents_(0)
{}

Channel::~Channel()
{
//   assert(!eventHandling_);
//   if (loop_->IsInLoopThread()){
//     assert(!loop_->HasChannel(this));
//   }
}


void Channel::HandleEvents()
{
    if ((revents_ & EPOLLHUP) && !(revents_ & EPOLLIN)){
        if (closeCallBack_) {
            closeCallBack_();
        }
        return;
    }

    if (revents_ & EPOLLERR){
        if (errorCallBack_){
            errorCallBack_();
        }
        return;
    }

    if (revents_ & (EPOLLIN | EPOLLPRI | EPOLLRDHUP)){
        readCallBack_();
    }

    if (revents_ & EPOLLOUT){
        writeCallBack_();
    }
}